﻿using System.Collections;
using System.Collections.Generic;

namespace Inlycat
{
    /// <summary>
    /// 实体管理器
    /// </summary>
    public class EntityManager
    {
        /// <summary>
        /// 实体表
        /// </summary>
        private Dictionary<int, BaseGameEntity> _entityDic;

        /// <summary>
        /// 注册实体
        /// </summary>
        /// <param name="entity">实体</param>
        public void RegisterEntity(BaseGameEntity entity)
        {
            _entityDic[entity.ID] = entity;
        }

        /// <summary>
        /// 移除实体
        /// </summary>
        /// <param name="entity">实体</param>
        public void RemoveEntity(BaseGameEntity entity)
        {
            _entityDic.Remove(entity.ID);
        }

        /// <summary>
        /// 移除所有实体
        /// </summary>
        public void RemoveAllEntities()
        {
            _entityDic.Clear();
        }

        /// <summary>
        /// 通过ID获取实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>实体</returns>
        public BaseGameEntity GetEntityFromID(int id)
        {
            if (_entityDic.ContainsKey(id))
            {
                return _entityDic[id];
            }
            return null;
        }

        /// <summary>
        /// 下个实体ID，用于自增长
        /// </summary>
        private static int _nextValidEntityId = 1;

        /// <summary>
        /// 获取一个新的实体ID
        /// </summary>
        /// <returns>新实体ID</returns>
        public static int GetNewEntityId()
        {
            return _nextValidEntityId++;
        }

        /// <summary>
        /// 单例
        /// </summary>
        private static EntityManager _instance;

        /// <summary>
        /// 单例
        /// </summary>
        public static EntityManager Instance
        {
            get { return _instance = _instance ?? new EntityManager(); }
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        private EntityManager()
        {
            _entityDic = new Dictionary<int, BaseGameEntity>();
        }
    }
}

